home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 320 / compsrc2 / global-a.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  28.0 KB  |  893 lines

  1. /* Allocate registers for pseudo-registers that span basic blocks.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include "config.h"
  24. #include "rtl.h"
  25. #include "flags.h"
  26. #include "basic-block.h"
  27. #include "hard-reg-set.h"
  28. #include "regs.h"
  29. #include "insn-config.h"
  30.  
  31. /* This pass of the compiler performs global register allocation.
  32.    It assigns hard register numbers to all the pseudo registers
  33.    that were not handled in local_alloc.  Assignments are recorded
  34.    in the vector reg_renumber, not by changing the rtl code.
  35.    (Such changes are made by final).  The entry point is
  36.    the function global_alloc.
  37.  
  38.    After allocation is complete, the reload pass is run as a subroutine
  39.    of this pass, so that when a pseudo reg loses its hard reg due to
  40.    spilling it is possible to make a second attempt to find a hard
  41.    reg for it.  The reload pass is independent in other respects
  42.    and it is run even when stupid register allocation is in use.
  43.  
  44.    1. count the pseudo-registers still needing allocation
  45.    and assign allocation-numbers (allocnos) to them.
  46.    Set up tables reg_allocno and allocno_reg to map 
  47.    reg numbers to allocnos and vice versa.
  48.    max_allocno gets the number of allocnos in use.
  49.  
  50.    2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
  51.    Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
  52.    for conflicts between allocnos and explicit hard register use
  53.    (which includes use of pseudo-registers allocated by local_alloc).
  54.  
  55.    3. for each basic block
  56.     walk forward through the block, recording which
  57.     unallocated registers and which hardware registers are live.
  58.     Build the conflict matrix between the unallocated registers
  59.     and another of unallocated registers versus hardware registers.
  60.     Someday also record the preferred hardware registers
  61.     for each unallocated one.
  62.  
  63.    4. Sort a table of the allocnos into order of
  64.    desirability of the variables.
  65.  
  66.    5. Allocate the variables in that order; each if possible into
  67.    an preferred register, else into another register.  */
  68.  
  69. /* Number of pseudo-registers still requiring allocation
  70.    (not allocated by local_allocate).  */
  71.  
  72. static int max_allocno;
  73.  
  74. /* Indexed by (pseudo) reg number, gives the allocno, or -1
  75.    for pseudo registers already allocated by local_allocate.  */
  76.  
  77. static int *reg_allocno;
  78.  
  79. /* Indexed by allocno, gives the reg number.  */
  80.  
  81. static int *allocno_reg;
  82.  
  83. /* A vector of the integers from 0 to max_allocno-1,
  84.    sorted in the order of first-to-be-allocated first.  */
  85.  
  86. static int *allocno_order;
  87.  
  88. /* Indexed by an allocno, gives the number of consecutive
  89.    hard registers needed by that pseudo reg.  */
  90.  
  91. static int *allocno_size;
  92.  
  93. /* Indexed by allocno, gives number of preferred hard reg, or -1 if none.  */
  94.  
  95. static int *allocno_preferred_reg;
  96.  
  97. /* max_allocno by max_allocno array of bits,
  98.    recording whether two allocno's conflict (can't go in the same
  99.    hardware register).
  100.  
  101.    `conflicts' is not symmetric; a conflict between allocno's i and j
  102.    is recorded either in element i,j or in element j,i.  */
  103.  
  104. static int *conflicts;
  105.  
  106. /* Number of ints require to hold max_allocno bits.
  107.    This is the length of a row in `conflicts'.  */
  108.  
  109. static int allocno_row_words;
  110.  
  111. /* Two macros to test or store 1 in an element of `conflicts'.  */
  112.  
  113. #define CONFLICTP(I, J) \
  114.  (conflicts[(I) * allocno_row_words + (J) / INT_BITS]    \
  115.   & (1 << ((J) % INT_BITS)))
  116.  
  117. #define SET_CONFLICT(I, J) \
  118.  (conflicts[(I) * allocno_row_words + (J) / INT_BITS]    \
  119.   |= (1 << ((J) % INT_BITS)))
  120.  
  121. /* Set of hard regs currently live (during scan of all insns).  */
  122.  
  123. static HARD_REG_SET hard_regs_live;
  124.  
  125. /* Indexed by N, set of hard regs conflicting with allocno N.  */
  126.  
  127. static HARD_REG_SET *hard_reg_conflicts;
  128.  
  129. #if 0
  130. /* Indexed by N, set of hard regs preferred by allocno N.
  131.    This was intended to be used to make allocnos go into regs
  132.    that they are loaded from, when possible, to reduce register shuffling.  */
  133.  
  134. static HARD_REG_SET *hard_reg_preferences;
  135. #endif
  136.  
  137. /* Set of registers that global-alloc isn't supposed to use.  */
  138.  
  139. static HARD_REG_SET no_global_alloc_regs;
  140.  
  141. /* Test a bit in TABLE, a vector of HARD_REG_SETs,
  142.    for vector element I, and hard register number J.  */
  143.  
  144. #define REGBITP(TABLE, I, J)     TEST_HARD_REG_BIT (TABLE[I], J)
  145.  
  146. /* Set to 1 a bit in a vector of HARD_REG_SETs.  Works like REGBITP.  */
  147.  
  148. #define SET_REGBIT(TABLE, I, J)  SET_HARD_REG_BIT (TABLE[I], J)
  149.  
  150. /* Bit mask for allocnos live at current point in the scan.  */
  151.  
  152. static int *allocnos_live;
  153.  
  154. #define INT_BITS HOST_BITS_PER_INT
  155.  
  156. /* Test, set or clear bit number I in allocnos_live,
  157.    a bit vector indexed by allocno.  */
  158.  
  159. #define ALLOCNO_LIVE_P(I) \
  160.   (allocnos_live[(I) / INT_BITS] & (1 << ((I) % INT_BITS)))
  161.  
  162. #define SET_ALLOCNO_LIVE(I) \
  163.   (allocnos_live[(I) / INT_BITS] |= (1 << ((I) % INT_BITS)))
  164.  
  165. #define CLEAR_ALLOCNO_LIVE(I) \
  166.   (allocnos_live[(I) / INT_BITS] &= ~(1 << ((I) % INT_BITS)))
  167.  
  168. static int allocno_compare ();
  169. static void mark_reg_store ();
  170. static void mark_reg_live_nc ();
  171. static void mark_reg_death ();
  172. static void dump_conflicts ();
  173. static void find_reg ();
  174. static void global_conflicts ();
  175. static void record_conflicts ();
  176.  
  177.  
  178. /* Tables describing and classifying the hardware registers.  */
  179.  
  180. /* Perform allocation of pseudo-registers not allocated by local_alloc.
  181.    FILE is a file to output debugging information on,
  182.    or zero if such output is not desired.  */
  183.  
  184. void
  185. global_alloc (file)
  186.      FILE *file;
  187. {
  188.   register int i;
  189.  
  190.   max_allocno = 0;
  191.  
  192.   /* A machine may have certain hard registers that
  193.      are safe to use only within a basic block.  */
  194.  
  195.   CLEAR_HARD_REG_SET (no_global_alloc_regs);
  196. #if 0
  197.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  198.     if (OVERLAPPING_REGNO_P (i))
  199.       SET_HARD_REG_BIT (no_global_alloc_regs, i);
  200. #endif
  201.  
  202.   /* Establish mappings from register number to allocation number
  203.      and vice versa.  In the process, count the allocnos.  */
  204.  
  205.   reg_allocno = (int *) alloca (max_regno * sizeof (int));
  206.  
  207.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  208.     reg_allocno[i] = -1;
  209.  
  210.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  211.     /* Note that reg_live_length[i] < 0 indicates a "constant" reg
  212.        that we are supposed to refrain from putting in a hard reg.
  213.        -2 means do make an allocno but don't allocate it.  */
  214.     if (reg_n_refs[i] != 0 && reg_renumber[i] < 0 && reg_live_length[i] != -1)
  215.       {
  216.     reg_allocno[i] = max_allocno++;
  217.     if (reg_live_length[i] == 0)
  218.       abort ();
  219.       }
  220.     else
  221.       reg_allocno[i] = -1;
  222.  
  223.   allocno_reg = (int *) alloca (max_allocno * sizeof (int));
  224.   allocno_size = (int *) alloca (max_allocno * sizeof (int));
  225.   allocno_preferred_reg = (int *) alloca (max_allocno * sizeof (int));
  226.   bzero (allocno_size, max_allocno * sizeof (int));
  227.  
  228.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  229.     if (reg_allocno[i] >= 0)
  230.       {
  231.     allocno_reg[reg_allocno[i]] = i;
  232.     allocno_size[reg_allocno[i]] = PSEUDO_REGNO_SIZE (i);
  233.     allocno_preferred_reg[reg_allocno[i]] = -1;
  234.       }
  235.  
  236.   /* Allocate the space for the conflict tables.  */
  237.  
  238.   hard_reg_conflicts = (HARD_REG_SET *)
  239.     alloca (max_allocno * sizeof (HARD_REG_SET));
  240.   bzero (hard_reg_conflicts, max_allocno * sizeof (HARD_REG_SET));
  241.  
  242. #if 0
  243.   hard_reg_preferences = (HARD_REG_SET *)
  244.     alloca (max_allocno * sizeof (HARD_REG_SET));
  245.   bzero (hard_reg_preferences, max_allocno * sizeof (HARD_REG_SET));
  246. #endif
  247.  
  248.   allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
  249.  
  250.   conflicts = (int *)
  251.     alloca (max_allocno * allocno_row_words * sizeof (int));
  252.   bzero (conflicts, max_allocno * allocno_row_words * sizeof (int));
  253.  
  254.   allocnos_live = (int *) alloca (allocno_row_words * sizeof (int));
  255.  
  256.   /* If there is work to be done (at least one reg to allocate),
  257.      perform global conflict analysis and allocate the regs.  */
  258.  
  259.   if (max_allocno > 0)
  260.     {
  261.       /* Scan all the insns and compute the conflicts among allocnos
  262.      and between allocnos and hard regs.  */
  263.  
  264.       global_conflicts ();
  265.  
  266.       /* Determine the order to allocate the remaining pseudo registers.  */
  267.  
  268.       allocno_order = (int *) alloca (max_allocno * sizeof (int));
  269.       for (i = 0; i < max_allocno; i++)
  270.     allocno_order[i] = i;
  271.  
  272.       /* Default the size to 1, since allocno_compare uses it to divide by.  */
  273.  
  274.       for (i = 0; i < max_allocno; i++)
  275.     if (allocno_size[i] == 0)
  276.       allocno_size[i] = 1;
  277.  
  278.       qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
  279.  
  280.       if (file)
  281.     dump_conflicts (file);
  282.  
  283.       /* Try allocating them, one by one, in that order,
  284.      except for parameters marked with reg_live_length[regno] == -2.  */
  285.  
  286.       for (i = 0; i < max_allocno; i++)
  287.     if (reg_live_length[allocno_reg[allocno_order[i]]] >= 0)
  288.       {
  289.         /* If we have a preferred hard register, try that first.  */
  290.         if (allocno_preferred_reg[allocno_order[i]] >= 0)
  291.           {
  292.         find_reg (allocno_order[i], 0, 0,
  293.               allocno_preferred_reg[allocno_order[i]]);
  294.         if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
  295.           continue;
  296.           }
  297.         /* If we have more than one register class,
  298.            first try allocating in the class that is cheapest
  299.            for this pseudo-reg.  If that fails, try any reg.  */
  300.         if (N_REG_CLASSES > 1)
  301.           {
  302.         find_reg (allocno_order[i], 0, 0, -1);
  303.         if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
  304.           continue;
  305.           }
  306.         if (!reg_preferred_or_nothing (allocno_reg[allocno_order[i]]))
  307.           find_reg (allocno_order[i], 0, 1, -1);
  308.       }
  309.     }
  310.  
  311.   /* Do the reloads now while the allocno data still exist, so that we can
  312.      try to assign new hard regs to any pseudo regs that are spilled.  */
  313.  
  314.   if (n_basic_blocks > 0)
  315.     reload (basic_block_head[0], 1, file);
  316. }
  317.  
  318. /* Sort predicate for ordering the allocnos.
  319.    Returns -1 (1) if *v1 should be allocated before (after) *v2.  */
  320.  
  321. static int
  322. allocno_compare (v1, v2)
  323.      int *v1, *v2;
  324. {
  325.   register int r1 = allocno_reg[*v1];
  326.   register int r2 = allocno_reg[*v2];
  327.   register double v 
  328.     = ((double) (floor_log2 (reg_n_refs[r1]) * reg_n_refs[r1])
  329.        / (reg_live_length[r1] * allocno_size[*v1]))
  330.       - ((double) (floor_log2 (reg_n_refs[r2]) * reg_n_refs[r2])
  331.      / (reg_live_length[r2] * allocno_size[*v2]));
  332.   if (v < 0)
  333.     return 1;
  334.   if (v > 0)
  335.     return -1;
  336.   return 0;
  337. }
  338.  
  339. /* Scan the rtl code and record all conflicts in the conflict matrices.  */
  340.  
  341. static void
  342. global_conflicts ()
  343. {
  344.   register int b, i;
  345.   register rtx insn;
  346.   short *block_start_allocnos;
  347.  
  348.   block_start_allocnos = (short *) alloca (max_allocno * sizeof (short));
  349.  
  350.   for (b = 0; b < n_basic_blocks; b++)
  351.     {
  352.       bzero (allocnos_live, allocno_row_words * sizeof (int));
  353.  
  354.       /* Initialize table of registers currently live
  355.      to the state at the beginning of this basic block.
  356.      This also marks the conflicts among them.
  357.  
  358.      For pseudo-regs, there is only one bit for each one
  359.      no matter how many hard regs it occupies.
  360.      This is ok; we know the size from PSEUDO_REGNO_SIZE.
  361.      For explicit hard regs, we cannot know the size that way
  362.      since one hard reg can be used with various sizes.
  363.      Therefore, we must require that all the hard regs
  364.      implicitly live as part of a multi-word hard reg
  365.      are explicitly marked in basic_block_live_at_start.  */
  366.  
  367.       {
  368.     register int offset, bit;
  369.     register regset old = basic_block_live_at_start[b];
  370.     int ax = 0;
  371.  
  372. #ifdef HARD_REG_SET
  373.     hard_regs_live = old[0];
  374. #else
  375.     COPY_HARD_REG_SET (hard_regs_live, old);
  376. #endif
  377.     for (offset = 0, i = 0; offset < regset_size; offset++)
  378.       if (old[offset] == 0)
  379.         i += HOST_BITS_PER_INT;
  380.       else
  381.         for (bit = 1; bit; bit <<= 1, i++)
  382.           {
  383.         if (i >= max_regno)
  384.           break;
  385.         if (old[offset] & bit)
  386.           {
  387.             register int a = reg_allocno[i];
  388.             if (a >= 0)
  389.               {
  390.             SET_ALLOCNO_LIVE (a);
  391.             block_start_allocnos[ax++] = a;
  392.               }
  393.             else if ((a = reg_renumber[i]) >= 0)
  394.               mark_reg_live_nc (a, PSEUDO_REGNO_MODE (i));
  395.           }
  396.           }
  397.  
  398.     /* Record that each allocno now live conflicts with each other
  399.        allocno now live, and with each hard reg now live.  */
  400.  
  401.     record_conflicts (block_start_allocnos, ax);
  402.       }
  403.  
  404.       insn = basic_block_head[b];
  405.  
  406.       /* Scan the code of this basic block, noting which allocnos
  407.      and hard regs are born or die.  When one is born,
  408.      record a conflict with all others currently live.  */
  409.  
  410.       while (1)
  411.     {
  412.       register RTX_CODE code = GET_CODE (insn);
  413.       register rtx link;
  414.       rtx regs_set[MAX_SETS_PER_INSN + MAX_CLOBBERS_PER_INSN];
  415.       int n_regs_set = 0;
  416.  
  417.       if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
  418.         {
  419.           /* Mark any registers dead after INSN as dead now.  */
  420.  
  421.           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  422.         if (REG_NOTE_KIND (link) == REG_DEAD)
  423.           mark_reg_death (XEXP (link, 0));
  424.  
  425.           /* Mark any registers set in INSN as live,
  426.          and mark them as conflicting with all other live regs.  */
  427.  
  428.           if ((GET_CODE (PATTERN (insn)) == SET
  429.            || GET_CODE (PATTERN (insn)) == CLOBBER)
  430.           && GET_CODE (SET_DEST (PATTERN (insn))) == REG)
  431.         {
  432.           register rtx z = SET_DEST (PATTERN (insn));
  433.           regs_set[n_regs_set++] = z;
  434.           mark_reg_store (z);
  435.         }
  436.           if ((GET_CODE (PATTERN (insn)) == SET
  437.            || GET_CODE (PATTERN (insn)) == CLOBBER)
  438.           && GET_CODE (SET_DEST (PATTERN (insn))) == SUBREG)
  439.         {
  440.           register rtx z = SUBREG_REG (SET_DEST (PATTERN (insn)));
  441.           if (GET_CODE (z) == REG)
  442.             {
  443.               regs_set[n_regs_set++] = z;
  444.               mark_reg_store (z);
  445.             }
  446.         }
  447.           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  448.         {
  449.           register rtx y = PATTERN (insn);
  450.           for (i = XVECLEN (y, 0) - 1;
  451.                i >= 0; i--)
  452.             if (GET_CODE (XVECEXP (y, 0, i)) == SET
  453.             || GET_CODE (XVECEXP (y, 0, i)) == CLOBBER)
  454.               {
  455.             rtx z = SET_DEST (XVECEXP (y, 0, i));
  456.             if (GET_CODE (z) == REG)
  457.               {
  458.                 regs_set[n_regs_set++] = z;
  459.                 mark_reg_store (z);
  460.               }
  461.             if (GET_CODE (z) == SUBREG
  462.                 && GET_CODE (SUBREG_REG (z)) == REG)
  463.               {
  464.                 regs_set[n_regs_set++] = SUBREG_REG (z);
  465.                 mark_reg_store (SUBREG_REG (z));
  466.               }
  467.               }
  468.         }
  469.  
  470.           /* Mark any registers both set and dead after INSN as dead.
  471.          This is not redundant!
  472.          A register may be set and killed in the same insn.
  473.          It is necessary to mark them as live, above, to get
  474.          the right conflicts within the insn.  */
  475.  
  476.           while (n_regs_set > 0)
  477.         if (find_regno_note (insn, REG_DEAD, REGNO (regs_set[--n_regs_set])))
  478.           mark_reg_death (regs_set[n_regs_set]);
  479.         
  480.           /* Likewise for regs set by incrementation.  */
  481.  
  482.           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  483.         if (REG_NOTE_KIND (link) == REG_INC
  484.             && find_regno_note (insn, REG_DEAD, REGNO (XEXP (link, 0))))
  485.           mark_reg_death (XEXP (link, 0));
  486.         }
  487.  
  488.       if (insn == basic_block_end[b])
  489.         break;
  490.       insn = NEXT_INSN (insn);
  491.     }
  492.     }
  493. }
  494.  
  495. /* Assign a hard register to ALLOCNO; look for one that is the beginning
  496.    of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
  497.    If PREFREG is >= 0, try that hard reg first.
  498.  
  499.    If ALL_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
  500.    Otherwise ignore that preferred class.
  501.  
  502.    If we find one, record it in reg_renumber.
  503.    If not, do nothing.  */
  504.  
  505. static void
  506. find_reg (allocno, losers, all_regs_p, prefreg)
  507.      int allocno;
  508.      register short *losers;
  509.      int all_regs_p;
  510.      int prefreg;
  511. {
  512.   register int i;
  513. #ifdef HARD_REG_SET
  514.   register        /* Declare it register if it's a scalar.  */
  515. #endif
  516.     HARD_REG_SET used;
  517.  
  518.   enum reg_class class 
  519.     = all_regs_p ? GENERAL_REGS : reg_preferred_class (allocno_reg[allocno]);
  520.   enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
  521.  
  522.   COPY_HARD_REG_SET (used,
  523.              (reg_crosses_call[allocno_reg[allocno]]
  524.               ? call_used_reg_set : fixed_reg_set));
  525.  
  526.   /* Some registers should not be allocated in global-alloc.  */
  527.   IOR_HARD_REG_SET (used, no_global_alloc_regs);
  528.  
  529.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  530.   IOR_HARD_REG_SET (used, hard_reg_conflicts[allocno]);
  531.   if (frame_pointer_needed)
  532.     SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
  533.  
  534.   /* If a preferred register was specified, try it first.  */
  535.  
  536.   i = -1;
  537.   if (prefreg >= 0)
  538.     if (! TEST_HARD_REG_BIT (used, prefreg)
  539.     && (losers == 0 || losers[prefreg] < 0)
  540.     && HARD_REGNO_MODE_OK (prefreg, mode))
  541.       {
  542.     register int j;
  543.     register int lim = prefreg + HARD_REGNO_NREGS (prefreg, mode);
  544.     for (j = prefreg + 1;
  545.          (j < lim
  546.           && ! TEST_HARD_REG_BIT (used, j)
  547.           && (losers == 0 || losers[j] < 0));
  548.          j++);
  549.     if (j == lim)
  550.       i = prefreg;
  551.       }
  552.  
  553.   /* Otherwise try each hard reg to see if it fits.  */
  554.  
  555.   if (i < 0)
  556.     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  557.       if (! TEST_HARD_REG_BIT (used, i)
  558.       && (losers == 0 || losers[i] < 0)
  559.       && HARD_REGNO_MODE_OK (i, mode))
  560.     {
  561.       register int j;
  562.       register int lim = i + HARD_REGNO_NREGS (i, mode);
  563.       for (j = i + 1;
  564.            (j < lim
  565.         && ! TEST_HARD_REG_BIT (used, j)
  566.         && (losers == 0 || losers[j] < 0));
  567.            j++);
  568.       if (j == lim)
  569.         break;
  570.       i = j;            /* Skip starting points we know will lose */
  571.     }
  572.  
  573.   /* Did we find a register?  */
  574.  
  575.   if (i < FIRST_PSEUDO_REGISTER)
  576.     {
  577.       register int lim, j;
  578.       HARD_REG_SET this_reg;
  579.  
  580.       /* Yes.  Record it as the hard register of this pseudo-reg.  */
  581.       reg_renumber[allocno_reg[allocno]] = i;
  582.       /* For each other pseudo-reg conflicting with this one,
  583.      mark it as conflicting with the hard regs this one occupies.  */
  584.       CLEAR_HARD_REG_SET (this_reg);
  585.       lim = i + HARD_REGNO_NREGS (i, mode);
  586.       for (j = i; j < lim; j++)
  587.     SET_HARD_REG_BIT (this_reg, j);
  588.       lim = allocno;
  589.       for (j = 0; j < max_allocno; j++)
  590.     if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
  591.       {
  592.         IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
  593.       }
  594.     }
  595. }
  596.  
  597. /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
  598.    Perhaps it had previously seemed not worth a hard reg,
  599.    or perhaps its old hard reg has been commandeered for reloads.
  600.    FORBIDDEN_REGS is a vector that indicates certain hard regs
  601.    that may not be used, even if they do not appear to be allocated.
  602.    A nonnegative element means the corresponding hard reg is forbidden.
  603.    If FORBIDDEN_REGS is zero, no regs are forbidden.  */
  604.  
  605. void
  606. retry_global_alloc (regno, forbidden_regs)
  607.      int regno;
  608.      short *forbidden_regs;
  609. {
  610.   int allocno = reg_allocno[regno];
  611.   if (allocno >= 0)
  612.     {
  613.       /* If we have more than one register class,
  614.      first try allocating in the class that is cheapest
  615.      for this pseudo-reg.  If that fails, try any reg.  */
  616.       if (N_REG_CLASSES > 1)
  617.     find_reg (allocno, forbidden_regs, 0, -1);
  618.       if (reg_renumber[regno] < 0
  619.       && !reg_preferred_or_nothing (regno))
  620.     find_reg (allocno, forbidden_regs, 1, -1);
  621.     }
  622. }
  623.  
  624. /* Called from reload pass to see if current function's pseudo regs
  625.    require a frame pointer to be allocated and set up.
  626.  
  627.    Return 1 if so, 0 otherwise.
  628.    We may alter the hard-reg allocation of the pseudo regs
  629.    in order to make the frame pointer unnecessary.
  630.    However, if the value is 1, nothing has been altered.
  631.  
  632.    Args grant access to some tables used in reload1.c.
  633.    See there for info on them.  */
  634.  
  635. int
  636. check_frame_pointer_required (reg_equiv_constant, reg_equiv_mem)
  637.      rtx *reg_equiv_constant, *reg_equiv_mem;
  638. {
  639.   register int i;
  640.   HARD_REG_SET *old_hard_reg_conflicts;
  641.   short *old_reg_renumber;
  642.   char old_regs_ever_live[FIRST_PSEUDO_REGISTER];
  643.  
  644.   /* If any pseudo reg has no hard reg and no equivalent,
  645.      we must have a frame pointer.  */
  646.  
  647.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  648.     if (reg_renumber[i] < 0 && reg_equiv_mem[i] == 0
  649.     && reg_equiv_constant[i] == 0 && reg_n_refs[i] > 0)
  650.       return 1;
  651.  
  652.   /* If we might not need a frame pointer,
  653.      try finding a hard reg for any pseudo that has a memory equivalent.
  654.      That is because the memory equivalent probably refers to a frame
  655.      pointer.  */
  656.  
  657.   old_reg_renumber = (short *) alloca (max_regno * sizeof (short));
  658.   old_hard_reg_conflicts = (HARD_REG_SET *)
  659.     alloca (max_allocno * sizeof (HARD_REG_SET));
  660.  
  661.   bcopy (reg_renumber, old_reg_renumber, max_regno * sizeof (short));
  662.   bcopy (hard_reg_conflicts, old_hard_reg_conflicts,
  663.      max_allocno * sizeof (HARD_REG_SET));
  664.   bcopy (regs_ever_live, old_regs_ever_live, sizeof regs_ever_live);
  665.  
  666.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  667.     if (reg_renumber[i] < 0 && reg_equiv_mem[i] != 0)
  668.       {
  669.     retry_global_alloc (i, 0);
  670.     /* If we can't find a hard reg for ALL of them,
  671.        or if a previously unneeded hard reg is used that requires saving,
  672.        we fail: set all those pseudos back as they were.  */
  673.     if (reg_renumber[i] < 0
  674.         || (! old_regs_ever_live[reg_renumber[i]]
  675.         && ! call_used_regs[reg_renumber[i]]))
  676.       {
  677.         bcopy (old_reg_renumber, reg_renumber,
  678.            max_regno * sizeof (short));
  679.         bcopy (old_hard_reg_conflicts, hard_reg_conflicts,
  680.            max_allocno * sizeof (HARD_REG_SET));
  681.         bcopy (old_regs_ever_live, regs_ever_live, sizeof regs_ever_live);
  682.         return 1;
  683.       }
  684.     mark_home_live (i);
  685.       }
  686.  
  687.   return 0;
  688. }
  689.  
  690. /* Record a conflict between register REGNO
  691.    and everything currently live.
  692.    REGNO must not be a pseudo reg that was allocated
  693.    by local_alloc; such numbers must be translated through
  694.    reg_renumber before calling here.  */
  695.  
  696. static void
  697. record_one_conflict (regno)
  698.      int regno;
  699. {
  700.   register int j;
  701.  
  702.   if (regno < FIRST_PSEUDO_REGISTER)
  703.     /* When a hard register becomes live,
  704.        record conflicts with live pseudo regs.  */
  705.     for (j = 0; j < max_allocno; j++)
  706.       {
  707.     if (ALLOCNO_LIVE_P (j))
  708.       SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
  709.       }
  710.   else
  711.     /* When a pseudo-register becomes live,
  712.        record conflicts first with hard regs,
  713.        then with other pseudo regs.  */
  714.     {
  715.       register int ialloc = reg_allocno[regno];
  716.       register int ialloc_prod = ialloc * allocno_row_words;
  717.       IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
  718.       for (j = allocno_row_words - 1; j >= 0; j--)
  719.     conflicts[ialloc_prod + j] |= allocnos_live[j];
  720.     }
  721. }
  722.  
  723. /* Record all allocnos currently live as conflicting
  724.    with each other and with all hard regs currently live.
  725.    ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
  726.    are currently live.  Their bits are also flagged in allocnos_live.  */
  727.  
  728. static void
  729. record_conflicts (allocno_vec, len)
  730.      register short *allocno_vec;
  731.      register int len;
  732. {
  733.   register int allocno;
  734.   register int j;
  735.   register int ialloc_prod;
  736.  
  737.   while (--len >= 0)
  738.     {
  739.       allocno = allocno_vec[len];
  740.       ialloc_prod = allocno * allocno_row_words;
  741.       IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
  742.       for (j = allocno_row_words - 1; j >= 0; j--)
  743.     conflicts[ialloc_prod + j] |= allocnos_live[j];
  744.     }
  745. }
  746.  
  747. /* Handle the case where REG is set by the insn being scanned,
  748.    during the forward scan to accumulate conflicts.
  749.    Store a 1 in regs_live or allocnos_live for this register, record how many
  750.    consecutive hardware registers it actually needs,
  751.    and record a conflict with all other registers already live.
  752.  
  753.    Note that even if REG does not remain alive after this insn,
  754.    we must mark it here as live, to ensure a conflict between
  755.    REG and any other regs set in this insn that really do live.
  756.    This is because those other regs could be considered after this.  */
  757.  
  758. static void
  759. mark_reg_store (reg)
  760.      rtx reg;
  761. {
  762.   register int regno = REGNO (reg);
  763.  
  764.   if (reg_renumber[regno] >= 0)
  765.     regno = reg_renumber[regno];
  766.  
  767.   /* Either this is one of the max_allocno pseudo regs not allocated,
  768.      or it is or has a hardware reg.  First handle the pseudo-regs.  */
  769.   if (regno >= FIRST_PSEUDO_REGISTER)
  770.     {
  771.       if (reg_allocno[regno] >= 0)
  772.     {
  773.       SET_ALLOCNO_LIVE (reg_allocno[regno]);
  774.       record_one_conflict (regno);
  775.     }
  776.     }
  777.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  778.   else if (! fixed_regs[regno])
  779.     {
  780.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  781.       while (regno < last)
  782.     {
  783.       record_one_conflict (regno);
  784.       SET_HARD_REG_BIT (hard_regs_live, regno);
  785.       regno++;
  786.     }
  787.     }
  788. }
  789.  
  790. /* Mark REG as being dead (following the insn being scanned now).
  791.    Store a 0 in regs_live or allocnos_live for this register.  */
  792.  
  793. static void
  794. mark_reg_death (reg)
  795.      rtx reg;
  796. {
  797.   register int regno = REGNO (reg);
  798.  
  799.   /* For pseudo reg, see if it has been assigned a hardware reg.  */
  800.   if (reg_renumber[regno] >= 0)
  801.     regno = reg_renumber[regno];
  802.  
  803.   /* Either this is one of the max_allocno pseudo regs not allocated,
  804.      or it is a hardware reg.  First handle the pseudo-regs.  */
  805.   if (regno >= FIRST_PSEUDO_REGISTER)
  806.     {
  807.       if (reg_allocno[regno] >= 0)
  808.     CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
  809.     }
  810.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  811.   else if (! fixed_regs[regno])
  812.     {
  813.       /* Pseudo regs already assigned hardware regs are treated
  814.      almost the same as explicit hardware regs.  */
  815.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  816.       while (regno < last)
  817.     {
  818.       CLEAR_HARD_REG_BIT (hard_regs_live, regno);
  819.       regno++;
  820.     }
  821.     }
  822. }
  823.  
  824. /* Mark hard reg REGNO as currently live, assuming machine mode MODE
  825.    for the value stored in it.  MODE determines how many consecutive
  826.    registers are actually in use.  Do not record conflicts;
  827.    it is assumed that the caller will do that.  */
  828.  
  829. static void
  830. mark_reg_live_nc (regno, mode)
  831.      register int regno;
  832.      enum machine_mode mode;
  833. {
  834.   register int last = regno + HARD_REGNO_NREGS (regno, mode);
  835.   while (regno < last)
  836.     {
  837.       SET_HARD_REG_BIT (hard_regs_live, regno);
  838.       regno++;
  839.     }
  840. }
  841.  
  842. /* Print debugging trace information if -greg switch is given,
  843.    showing the information on which the allocation decisions are based.  */
  844.  
  845. static void
  846. dump_conflicts (file)
  847.      FILE *file;
  848. {
  849.   register int i;
  850.   fprintf (file, ";; %d regs to allocate:", max_allocno);
  851.   for (i = 0; i < max_allocno; i++)
  852.     {
  853.       fprintf (file, " %d", allocno_reg[allocno_order[i]]);
  854.       if (allocno_size[allocno_order[i]] != 1)
  855.     fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
  856.     }
  857.   fprintf (file, "\n");
  858.  
  859.   for (i = 0; i < max_allocno; i++)
  860.     {
  861.       register int j;
  862.       fprintf (file, ";; %d conflicts:", allocno_reg[i]);
  863.       for (j = 0; j < max_allocno; j++)
  864.     if (CONFLICTP (i, j) || CONFLICTP (j, i))
  865.       fprintf (file, " %d", allocno_reg[j]);
  866.       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
  867.     if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
  868.       fprintf (file, " %d", j);
  869.       fprintf (file, "\n");
  870.     }
  871.   fprintf (file, "\n");
  872. }
  873.  
  874. void
  875. dump_global_regs (file)
  876.      FILE *file;
  877. {
  878.   register int i;
  879.  
  880.   fprintf (file, ";; Register dispositions:");
  881.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  882.     {
  883.       if (reg_renumber[i] >= 0)
  884.     fprintf (file, " %d in %d ", i, reg_renumber[i]);
  885.     }
  886.  
  887.   fprintf (file, "\n\n;; Hard regs used: ");
  888.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  889.     if (regs_ever_live[i])
  890.       fprintf (file, " %d", i);
  891.   fprintf (file, "\n\n");
  892. }
  893.